// login module
import { makeAutoObservable } from 'mobx'
import { setToken, getToken, removeToken, client } from '@/utils'
import { gql } from '@apollo/client'
const GET_LOGIN_TOKEN = gql`
mutation Login($username: String!, $password: String!) {
  login(username: $username, password: $password) {
    id
    token
    username
    phone
    type
    name
  }
}
`
class LoginStore {
  token = getToken() || ''
  constructor() {
    // 响应式
    makeAutoObservable(this)
  }
  getToken = async ({ username, password }) => {
    const { data: { login: { token } } } = await client.mutate({
      mutation: GET_LOGIN_TOKEN,
      variables: {
        username,
        password
      }
    })
    this.token = token
    // 存入ls
    setToken(this.token)
  }
  // 退出登录
  loginOut = () => {
    this.token = ''
    removeToken()
  }
}

export default LoginStore